package com.tsugun.mediaplayer.view.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import com.tsugun.mediaplayer.R;
import com.tsugun.mediaplayer.presenter.utils.SettingPreferenceHelper;

import java.util.List;

/**
 * 音乐菜单适配器
 * Created by shize on 2017/6/5.
 */

public class MusicMenuAdapter extends RecyclerView.Adapter<MusicMenuAdapter.MenuViewHolder> {

    private List<String> mTitles; // 标题和类型
    private int[] mIcons; // 菜单图标
    private LayoutInflater mInflater;
    private OnMenuItemClickListener mListener;

    public MusicMenuAdapter(Context context, List<String> titles, int[] mIcons) {
        mInflater = LayoutInflater.from(context);
        this.mIcons = mIcons;
        this.mTitles = titles;
    }

    @Override

    public MenuViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MenuViewHolder(mInflater.inflate(R.layout.item_music_menu, parent, false));
    }

    @Override
    public void onBindViewHolder(final MenuViewHolder holder, int position) {
        holder.mTvTitle.setText(mTitles.get(position));
        holder.mIvIcon.setImageResource(mIcons[position]);
        if (mListener == null) {
            return;
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onMenuClick(holder.getAdapterPosition());
            }
        });

        setSettingTheme(holder);
    }

    // 设置主题色
    private void setSettingTheme(MenuViewHolder holder) {
        holder.mTvTitle.setTextColor(SettingPreferenceHelper.INSTANCE.getTEXT_COLOR());
    }

    @Override
    public int getItemCount() {
        return mTitles.size();
    }

    /**
     * 设置菜单项点击事件
     */
    public void setOnMenuClickListener(OnMenuItemClickListener listener) {
        mListener = listener;
    }

    class MenuViewHolder extends RecyclerView.ViewHolder {

        private ImageButton mIvIcon;
        private TextView mTvTitle;

        MenuViewHolder(View itemView) {
            super(itemView);
            mIvIcon = (ImageButton) itemView.findViewById(R.id.id_item_music_menu_icon);
            mTvTitle = (TextView) itemView.findViewById(R.id.id_item_music_menu_title);
        }
    }

    public interface OnMenuItemClickListener {
        /**
         * 菜单点击事件
         *
         * @param pos 菜单项位置
         */
        void onMenuClick(int pos);
    }
}
